#include"add.h"

//信号量
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void* consum(void* args)
{

    annular_queue<int> *aq=static_cast<annular_queue<int> *>(args);
    int ret;
    while(1)
    {
        aq->pop(&ret);
        cout<<"get a data: "<<ret<<endl;
    }

}
void* produce(void* args)
{
    
    annular_queue<int> *aq=static_cast<annular_queue<int> *>(args);
    int k=100;//公共资源
    while(k)
    {
        pthread_mutex_lock(&mutex);
        aq->push(k);
        cout<<"produce a data: "<<k<<endl;
        k--;
        usleep(10000);
        pthread_mutex_unlock(&mutex);
    }
}
int main()
{
    pthread_t tid_1,tid_2,tid_3,tid_4,tid_5;
    annular_queue<int> *aq=new annular_queue<int> (5);
    pthread_create(&tid_1,nullptr,consum,aq);
    pthread_create(&tid_2,nullptr,consum,aq);
    pthread_create(&tid_3,nullptr,produce,aq);
    pthread_create(&tid_4,nullptr,produce,aq);
    pthread_create(&tid_5,nullptr,produce,aq);

    pthread_join(tid_1,nullptr);
    pthread_join(tid_2,nullptr);

    return 0;
}